﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace System.Web.Mvc
{
    /// <summary>
    /// 返回javascript Result 调用方式 return ScriptResult("alert('test')")
    /// </summary>
    public class ScriptResult : ActionResult
    {
        public string Scripts { get; set; }
        public override void ExecuteResult(ControllerContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            HttpResponseBase response = context.HttpContext.Response;
            if (this.Scripts != null)
            {
                response.Write("<script type=\"text/javascript\">" + this.Scripts + "</script>");
            }
        }
    }

    public class AjaxLoadResult : ScriptResult
    {
        public AjaxLoadResult(string targetHtmlID, string url, string jsFunctionCallback)
        {
            this.Scripts = string.Format("$('#{0}').load('{1}',function(){{2}})", targetHtmlID, url, jsFunctionCallback);
        }
        public AjaxLoadResult(string targetHtmlID, string url)
        {
            this.Scripts = string.Format("$('#{0}').load('{1}')", targetHtmlID, url);
        }
        public AjaxLoadResult(string url)
        {
            this.Scripts = string.Format("$('#main').load('{0}')", url);
        }
    }
}
